<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class PermissionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $pers = Permission::paginate(10);
        return view("admin.permission.list",compact('pers'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.permission.per_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->except('_token');
//       1.数据合法性验证
        $rule = [
            'per_name' => 'required',
            'per_url' => 'required',
        ];
        $messages = [
            'per_name.required' => '权限名称不能为空!',
            'per_url.required' => '权限规则不能为空!'
        ];

        $validator = Validator::make($request->all(),$rule,$messages);
        if($validator->fails()){
            $errors = $validator->errors()->toArray();
            $res = [
                'status'=>1,
                'data'=>$errors,
                'message'=>'验证失败'
            ];
            return json_encode($res);
        }
        //保存数据到数据库
        $per = new Permission();
        //判断用户是否已经注册
        $perinfo = $per->where('per_name',$input['per_name'])->first();
        if(!empty($perinfo)){
            $res = [
                'status'=>1,
                'data'=>['权限名称已经存在！'],
                'message'=>'添加失败'
            ];
            return json_encode($res);
        }
        $per->per_name = $input['per_name'];
        $per->per_url = $input['per_url'];
        $status = $per->save();
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'添加成功'
            ];
        }else{
            $res = [
                'status'=>1,
                'data'=>['添加失败，请检查数据库'],
                'message'=>'添加失败'
            ];
        }
        return json_encode($res);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $per = Permission::find($id);
        return view("admin.permission.per_edit",compact('per'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->except('_token');
//       1.数据合法性验证
        $rule = [
            'per_name' => 'required',
            'per_url' => 'required',
        ];
        $messages = [
            'per_name.required' => '权限名称不能为空!',
            'per_url.required' => '权限规则不能为空!'
        ];
        $validator = Validator::make($request->all(),$rule,$messages);
        if($validator->fails()){
            $errors = $validator->errors()->toArray();
            $res = [
                'status'=>1,
                'data'=>$errors,
                'message'=>'验证失败'
            ];

        }
        $per = new Permission();
        $status = $per->where('id',$input['id'])->update($input);
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'修改成功'
            ];

        }else{
            $res = [
                'status'=>1,
                'data'=>['修改失败'],
                'message'=>'修改失败'
            ];
        }
        return json_encode($res);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    //修改用户状态
    public function checkStatus(Request $request){
        $input = $request->except('_token');
        $per = new Permission();
        $status = $per->where('id',$input['id'])->update(['status'=>$input['status']]);
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'修改成功'
            ];

        }else{
            $res = [
                'status'=>1,
                'data'=>['修改失败！'],
                'message'=>'修改失败'
            ];
        }
        return json_encode($res);
    }
    //批量删除
    public function deleteAll(Request $request){

        $per = new per();
        $status = $per->whereIn('id',$request->get('ids'))->delete();
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'批量删除成功'
            ];

        }else{
            $res = [
                'status'=>1,
                'data'=>['批量删除失败！'],
                'message'=>'批量删除失败'
            ];
        }
        return json_encode($res);
    }
}
